package ranab.io;

import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.RandomAccessFile;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.Random;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.webdeninteractive.bie.commons.logging.SM;

/**
 * Log class to write log data. It uses <code>RandomAccessFile</code>.
 * If the log file size exceeds the limit, a new log file will be created.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public class BIELogFile extends LogFile {
    
    /**
     * Line separator
     */
    private final static String LINE_SEP = System.getProperty("line.separator", "\n");
    
    
    /**
     * Date format to backup old files. 
     */
    private DateFormat LOG_FILE_DATE = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
    
    /**
     * Debug log level - lowest priority.
     */
    public static final int DEBUG  = 0;
    
    /**
     * Normal log level - print messages.
     */
    public static final int INFO   = 1;
    
    /**
     * Warning message
     */
    public static final int WARN   = 2;    
    
    /**
     * Eror log level - print errors.
     */
    public static final int ERROR  = 3;
    
    private static final String mstLogHeader[] = { 
        "DEB",
        "INF",
        "WAR",
        "ERR"
    };

    private DateFormat mDateFmt = null;
    private Writer mOfs         = null;
    private File mLogFile       = null;
    private int miLogLevel      = INFO;
    private long mlMaxSize      = -1;    // no size limit
    private long mlSize         = 0;     // current size
    private boolean mbAutoFlush = false; 

    /**
     * Constructor. 
     */
    public BIELogFile(File logFile) {
    }

    public BIELogFile() {
        
    }
    
    /**
     * Set log date format
     */
    public void setDateFormat(DateFormat fmt)  {
        mDateFmt = fmt;
    }
    
    /**
     * Get the log date format.
     */
    public DateFormat getDateFormat() {
        return mDateFmt;
    }
    
    /**
     * Get auto flush
     */
    public boolean getAutoFlush() {
        return mbAutoFlush;
    }
    
    /**
     * Set auto flush
     */
    public void setAutoFlush(boolean flush) {
        mbAutoFlush = flush;
    }
    
    /**
     * Get log file.
     */
    public File getFile() {
        return mLogFile;
    }
     
    /**
     * Get max size.
     */
    public long getMaxSize() {
        return mlMaxSize;
    }
    
    /**
     * Set max file size.
     * Set <code>max</code> less than or equal
     * to zero to disable max size limitation.
     */
    public synchronized void setMaxSize(long max) {
        mlMaxSize = max;
    }

    /**
     * Get the current file size.
     */
    public long getCurrentSize() {
        return mlSize;
    }
    
    /**
     * Get current log level.
     */
    public int getLogLevel() {
        return miLogLevel;
    }
    
    
    /**
     * Open log file.
     */
    private synchronized void open() {
    }
    
    /**
     * Print debug log.
     * Here the log level is <code>DEBUG</code>.
     */
    public void debug(String str) {
        SM.debug(str);
        
        //write(DEBUG ,str);
    }
    
    /**
     * Print info log.
     */
    public void info(String str) {
        //write(INFO ,str);
    }
    
    /**
     * Print warning log.
     */
    public void warn(String str) {
        SM.warning(str);        
        //write(WARN ,str);
    }
    
    /**
     * Print info log.
     */
    public void error(String str) {
        SM.warning(str);
        //write(ERROR ,str);
    }
    
    /**
     * Print debug log.
     * Here the log level is <code>DEBUG</code>.
     */
    public void debug(Throwable th) {
        //write(DEBUG ,IoUtils.getStackTrace(th));
    }
    
    /**
     * Print info log.
     */
    public void info(Throwable th) {
        //write(INFO ,IoUtils.getStackTrace(th));
    }
    
    /**
     * Print warning log.
     */
    public void warn(Throwable th) {
        SM.warning(new Exception(th));
        //write(WARN ,IoUtils.getStackTrace(th));
    }
    
    /**
     * Print info log.
     */
    public void error(Throwable th) {
        SM.warning(new Exception(th));
        //write(ERROR ,IoUtils.getStackTrace(th));
    }
    
        
}